package org.example.controller.user;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.example.annotation.HasRole;
import org.example.dao.commom.Result;
import org.example.dao.commom.ResultCode;
import org.example.dao.dto.user.UAddressDto;
import org.example.dao.entity.user.UAddress;
import org.example.service.user.AddressServiceImpl;
import org.example.skytool.commom.ParamUtil;
import org.simpleframework.xml.Path;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * @author 22449
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class AddressController {

    /**
     * 服务对象
     */
    @Resource
    private AddressServiceImpl addressService;

    //TODO 新增

    /**
     * 添加收获地址(用户)
     */
    @RequestMapping(value = "addAddress", method = POST)
    @HasRole(role = "用户")
    public Result addAddress(@RequestBody UAddress address) {
        String title = address.getTitle();
        String schoolName = address.getSchoolName();
        String dormitoryBuilding = address.getDormitoryBuilding();
        String houseNumber = address.getHouseNumber();
        String name = address.getName();
        String mobile = address.getMobile();
        String province = address.getProvince();
        String city = address.getCity();
        String region = address.getRegion();

        ParamUtil.StringParam(title, schoolName, dormitoryBuilding,
                houseNumber, name, mobile, province, city, region);


        UAddress uAddress = addressService.addAddress(title, schoolName, dormitoryBuilding,
                houseNumber, name, mobile, province, city, region);
        return new Result(ResultCode.SUCCESS, uAddress);
    }

    //TODO 查询

    /**
     * 获取收获地址列表(用户)
     */
    @HasRole(role = "用户")
    @RequestMapping(value = "getAddress/{start}/{limit}", method = POST)
    public Result getAddress(@PathVariable("start") Integer start, @PathVariable("limit") Integer limit) {
        IPage<UAddressDto> list = addressService.getAddress(start,limit);
        return new Result(ResultCode.SUCCESS, list);
    }


    //TODO 更新

    /**
     * 修改收获地址(用户)
     */
    @HasRole(role = "用户")
    @RequestMapping(value = "updateAddress", method = POST)
    public Result updateAddress(@RequestBody UAddress address) {
        String title = address.getTitle();
        String schoolName = address.getSchoolName();
        String dormitoryBuilding = address.getDormitoryBuilding();
        String houseNumber = address.getHouseNumber();
        String name = address.getName();
        String mobile = address.getMobile();
        String province = address.getProvince();
        String city = address.getCity();
        String region = address.getRegion();
        Long id = address.getId();
        UAddress address1 = addressService.updateAddress(title, schoolName, dormitoryBuilding, houseNumber, name
                , mobile, province, city, region, id);
        return new Result(ResultCode.SUCCESS, address1);
    }

    //TODO 删除

    /**
     * 删除收获地址(用户)
     */
    @HasRole(role = "用户")
    @RequestMapping(value = "deleteAddress", method = POST)
    public Result deleteAddress(@RequestBody UAddress address) {
        Long id = address.getId();
        addressService.deleteAddress(id);
        return new Result(ResultCode.SUCCESS, null);
    }


}
